<?php
  include("include/config.inc.php");
  $waiterId    = $_REQUEST['waiterId'];
  $tableId         = $_REQUEST['tableId'];
  $quantity        = $_REQUEST['quantity'];
  $itemId          = $_REQUEST['itemId'];
  $tablePart       = $_REQUEST['tablePart'];
  $selectOrder = "SELECT orderId 
                    FROM ordermaster
                   WHERE waiterId=".$waiterId."
                     AND tableId=".$tableId."
                     AND tablePart='".$tablePart."'
                     AND orderstatus='L' ";
  $selectOrderRes = mysql_query($selectOrder);
  
  if($orderRow = mysql_fetch_array($selectOrderRes))
  {
	  $selectOrderItem = "SELECT orderedItemsId 
	                        FROM ordereditems  
	                       WHERE orderId = ".$orderRow['orderId']." 
	                         AND itemId  = ".$itemId;
	  $selectOrderItemRes = mysql_query($selectOrderItem);
	  
	  if($selectOrderItemRow = mysql_fetch_array($selectOrderItemRes))
	  {
      $updateOrderedItems = "UPDATE ordereditems
                                SET `quantity`     = ".$quantity.",
                                    `orderTime`    = NOW()
                              WHERE orderedItemsId = ".$selectOrderItemRow['orderedItemsId'];
      $updateOrderedItemsRes = mysql_query($updateOrderedItems);
 //===========     
      $checkZero = " SELECT quantity 
                       FROM ordereditems 
                      WHERE orderedItemsId = ".$selectOrderItemRow['orderedItemsId']." ";
      $checkZeroRes = mysql_query($checkZero);
      $checkZeroRow = mysql_fetch_array($checkZeroRes); 
      $checkZeroRow['quantity'];
      if($checkZeroRow['quantity']== 0)
      {
        $deleteOrederedItem  = " DELETE FROM ordereditems 
                                  WHERE ordereditemsId=".$selectOrderItemRow['orderedItemsId']." ";
        $deleteOrederedItemRes = mysql_query($deleteOrederedItem);
      }
    } 
    else
    {
    	$insertOrderedItem =  "INSERT INTO `ordereditems`(orderId,itemId,quantity,orderTime)
                             VALUES ( '".$orderRow['orderId']."', '".$itemId."','".$quantity."',NOW())";
      $insertOrderedItemRes = mysql_query($insertOrderedItem);
    }
  }
  else
  {
    // inserting value in orderMaster
    if($waiterId > 0)
    {
   echo    $insertOrder = "INSERT INTO `ordermaster`(`waiterId`,`tableId`,`tablePart`,`startTime`,`orderstatus`)
                      VALUES ( '".$waiterId."', '".$tableId."','".$tablePart."', NOW( ),'L')";
      $insertOrderRes = mysql_query($insertOrder);
      $lastOrderId    = mysql_insert_id();
      
      $insertOrderedItem =  "INSERT INTO `ordereditems`(`orderId`,`itemId`,`quantity`) 
                             VALUES ( '".$lastOrderId."', '".$itemId."','".$quantity."'))";
      $insertOrderedItemRes = mysql_query($insertOrderedItem);
      
      //select value from order MAster for display in icecream.tpl
      echo $selectItem = "SELECT itemName,itemPrice 
                       FROM item 
                      WHERE itemId = ".$itemId;
      $selectItemRes = mysql_query($selectItem);
      $row = mysql_fetch_array($selectItemRes);
    }
?>
  <tr>
 	  <td align="center"><a href="" onclick="return confirm('Are u sure to Delete?')"><img src="images/download.jpg" height="25px" width="25px"/></a></td>
 	  <td><?php echo $row['itemName']; ?></td>
 	  <td><?php echo $row['itemPrice']; ?></td>
 	  <td>
 	    <input type="button" name="plus" value=" + ">
      <input type="text" name="quenty" class="quntity" value="<?php echo $quantity; ?>">
      <input type="button" name="Minus" value=" - ">	
 	  </td>
 	</tr>
<?php 
  }
?>